﻿using System;
using System.Web.Mvc;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;

namespace WebSite.Authentication
{
    internal class DefaultOpenIdRelyingParty : IOpenIdRelyingParty, IDisposable
    {
        private readonly OpenIdRelyingParty relyingParty = new OpenIdRelyingParty();

        public IAuthenticationResponse GetResponse()
        {
            return this.relyingParty.GetResponse();
        }

        public ActionResult RedirectToProvider(string providerUrl, string returnUrl, FetchRequest fetch)
        {
            IAuthenticationRequest authenticationRequest = this.relyingParty.CreateRequest(providerUrl, Realm.AutoDetect, new Uri(returnUrl));
            authenticationRequest.AddExtension(fetch);
            return new OutgoingRequestActionResult(authenticationRequest.RedirectingResponse);
        }

        public virtual void Disponse()
        {
            this.relyingParty.Dispose();
        }
    }
}